package m.jclecai.com.ui;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import m.jclecai.com.MyApplication;
import m.jclecai.com.R;
import m.jclecai.com.bean.OpenListBean;
import m.jclecai.com.bean.klpk.bean.PokerBean;
import m.jclecai.com.utils.BaseUtils;
import m.jclecai.com.utils.KlpkUtils;

/**
 * Created by andy on 2017/8/25.
 */
public class KLPKHistoryAdapter extends BaseAdapter {


    private final List<OpenListBean.Records> mDataList;
    private final Context mContext;

    public KLPKHistoryAdapter(Context context, List<OpenListBean.Records> bean) {
        mDataList = bean;
        mContext = context;
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view=convertView;
        ViewHolder viewHolder;
        if(convertView == null){

            view = View.inflate(mContext, R.layout.item_klpk_history, null);
            viewHolder=new ViewHolder();
            viewHolder.tvTerm= ((TextView) view.findViewById(R.id.tv_term));
            viewHolder.tvPokerType= ((TextView) view.findViewById(R.id.tv_poker_type));
            viewHolder.tvPokerOne= ((TextView) view.findViewById(R.id.tv_history_poker_one));
            viewHolder.tvPokerTwo= ((TextView) view.findViewById(R.id.tv_history_poker_two));
            viewHolder.tvPokerThree= ((TextView) view.findViewById(R.id.tv_history_poker_three));

            view.setTag(viewHolder);
        }else{
           viewHolder = ((ViewHolder) view.getTag());
        }

        OpenListBean.Records records = mDataList.get(position);
        TextView[] pokers=new TextView[3];
        pokers[0]=viewHolder.tvPokerOne;
        pokers[1]=viewHolder.tvPokerTwo;
        pokers[2]=viewHolder.tvPokerThree;

        viewHolder.tvTerm.setText(records.getTerm().substring(records.getTerm().length()-2)+"期");

        setAnnouncementInfo(records.getResult(),pokers,viewHolder.tvPokerType);
        return view;
    }

    /**
     * 这里是设置快乐扑克的item的
     * @param results
     * @param pokers
     */
    private void setAnnouncementInfo(String results,TextView[] pokers,TextView tvPokerType){

        List<PokerBean> pokerBeanList = KlpkUtils.getPokerBeanList(results);

        MyApplication applicationContext = (MyApplication) mContext.getApplicationContext();

        //设置牌型；
        for (int i = 0; i < pokerBeanList.size(); i++) {

            TextView tvPoker=pokers[i];
            tvPoker.setTypeface(applicationContext.getPokerTypeFace());
            PokerBean pokerBean = pokerBeanList.get(i);
            PokerBean.Category category = pokerBean.getCategory();


            String pokerNumber = KlpkUtils.pokerNumer[pokerBean.getNumber()];
            tvPoker.setText(pokerNumber);
            tvPoker.setBackground(mContext.getResources().getDrawable(KlpkUtils.pokerHistoryBgRes[category.ordinal()]));

            //如果是10的话的，paddingRight=1dp
            if("10".equals(pokerNumber)){
                tvPoker.setPadding(0,0, BaseUtils.dip2px(mContext,1),0);
            }else{
                tvPoker.setPadding(0,0, BaseUtils.dip2px(mContext,5),0);
            }


            if(category== PokerBean.Category.box||category== PokerBean.Category.hearts){
                tvPoker.setTextColor(mContext.getResources().getColor(R.color.klpk_text_red_color));
            }else{
                tvPoker.setTextColor(Color.BLACK);
            }
        }
        //这里我们是需要判断牌型的；
        tvPokerType.setText("形态 : "+KlpkUtils.getBrandType(pokerBeanList));
    }



    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    //这里是相应的ViewHolder对象；
    public class ViewHolder{

        public TextView tvTerm;
        public TextView tvPokerType;

        public TextView tvPokerOne;
        public TextView tvPokerTwo;
        public TextView tvPokerThree;

    }
}
